<?php
App::uses('AppController', 'Controller');
/**
 * ArosAcos Controller
 *
 * @property ArosAco $ArosAco
 */
class ArosAcosController extends AppController {

	public $paginate = array(
	        'limit' => 25,
	        'order' => array(
	            'ArosAco.aro_id' => 'asc',
				'ArosAco.aco_id' => 'asc'
			)
	);
	
	public function beforeFilter() {
		parent::beforeFilter();
		//$this->Auth->allow('index','view');		
	}
	
	public function beforeRender(){
		parent::beforeRender();
		$this->layout = 'panel';
		$this->set('actions',array('create'=>__('Create',true),'read'=>__('Read',true),'update'=>__('Update',true),'delete'=>__('Delete',true),'*'=>__('All')));
	}
	
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->loadModel('Aco');
		$this->loadModel('Group');
		
		if ($this->request->is('post')) {
			$parents = $this->Aco->getPath($this->request->data['Aco']['aco_id'],array('Aco.alias'));
			$i = '';
			$a = '';
			foreach ($parents as $key => $value) {
				$a .= $i.$value['Aco']['alias'];
				if($i == '') $i ='/';
			}
			$this->Group->id = $this->request->data['Aco']['group_id'];
			if($this->request->data['Aco']['access']) $this->Acl->allow($this->Group, $a,$this->request->data['Aco']['action_id']);
			else $this->Acl->deny($this->Group, $a,$this->request->data['Aco']['action_id']);
			$this->redirect($this->referer());
		}
		
		$this->Aco->recursive = 0;
		$aco = $this->Aco->find('threaded');
		$acos = $this->treeForCombobox($aco,'Aco',array('model'=>'Aco','name'=>'alias'),false);
		
		
		$this->Group->recursive = 0;
		$groups = $this->Group->find('list');
		$this->set(compact('acos','groups'));
		
		$this->ArosAco->recursive = 0;
		$arosAcos = $this->paginate();
		foreach ($arosAcos as $key0 => $value) {
			$i = '';
			$p = '';
			$path = $this->Aco->getPath($value['Aco']['id']);
			foreach ($path as $key1 => $v) {
				$p .= $i.$v['Aco']['alias'];
				if($i == '') $i ='/';
			}
			$arosAcos[$key0]['Aco']['path'] = $p;
			$arosAcos[$key0]['Aro']['alias'] = $groups[$value['Aro']['foreign_key']];
			
		}
		
// 		debug($arosAcos);
		$this->set('arosAcos', $arosAcos);
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->ArosAco->id = $id;
		if (!$this->ArosAco->exists()) {
			throw new NotFoundException(__('Invalid aros aco'));
		}
		$this->set('arosAco', $this->ArosAco->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->ArosAco->create();
			if ($this->ArosAco->save($this->request->data)) {
				$this->Session->setFlash(__('The aros aco has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The aros aco could not be saved. Please, try again.'));
			}
		}
		$aros = $this->ArosAco->Aro->find('list');
		$acos = $this->ArosAco->Aco->find('list');
		$this->set(compact('aros', 'acos'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->ArosAco->id = $id;
		if (!$this->ArosAco->exists()) {
			throw new NotFoundException(__('Invalid aros aco'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->ArosAco->save($this->request->data)) {
				$this->Session->setFlash(__('The aros aco has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The aros aco could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->ArosAco->read(null, $id);
		}
		$aros = $this->ArosAco->Aro->find('list');
		$acos = $this->ArosAco->Aco->find('list');
		$this->set(compact('aros', 'acos'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->ArosAco->id = $id;
		if (!$this->ArosAco->exists()) {
			throw new NotFoundException(__('Invalid aros aco'));
		}
		if ($this->ArosAco->delete()) {
			$this->Session->setFlash(__('Aros aco deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Aros aco was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
